﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guline.Web.Central.Controllers;
using WebMatrix.WebData;
using System.Web.Security;
namespace Guline.Web.Central.Areas.User.Controllers
{
    public class DashboardController : BaseController//ke thua cai nay. co sẵn db.context
    {
        //
        // GET: /User/Dashboard/

        public ActionResult Index()
        {
            var acc = db.Accounts.Where(m => m.UserName == User.Identity.Name).FirstOrDefault();
            var token = WebSecurity.GeneratePasswordResetToken(User.Identity.Name);
            ViewBag.MK = token;
            var Days = acc.BirthDay.Day;
            var Months = acc.BirthDay.Month;
            var Years = acc.BirthDay.Year;
            List<Models.ListModel> lDay = new List<Models.ListModel>();
            for (int i = 1; i <= 31; i++)
            {
                lDay.Add(new Models.ListModel() { ID = i.ToString(), Name = i.ToString() });
            }
            ViewBag.Day = new SelectList(lDay, "ID", "Name", Days);

            List<Models.ListModel> lMonth = new List<Models.ListModel>();
            for (int i = 1; i <= 12; i++)
            {
                lMonth.Add(new Models.ListModel() { ID = i.ToString(), Name = i.ToString() });
            }
            ViewBag.Month = new SelectList(lMonth, "ID", "Name", Months);

            List<Models.ListModel> lYear = new List<Models.ListModel>();
            for (int i = DateTime.Now.Year - 5; i >= DateTime.Now.Year - 120; i--)
            {
                lYear.Add(new Models.ListModel() { ID = i.ToString(), Name = i.ToString() });
            }
            ViewBag.Year = new SelectList(lYear, "ID", "Name", Years);


            return View(acc);
        }
        [HttpPost]
        public ActionResult DisplayName_Change(int ID, string DisplayName)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var acc = db.Accounts.Where(m => m.ID == ID).FirstOrDefault();
                    acc.DisplayName = DisplayName;
                    db.SaveChanges();
                    return Json(acc.DisplayName, JsonRequestBehavior.AllowGet);
                }
            }
            catch
            {

            }
            return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult Name_Change(int ID, string FirstName, string LastName)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var acc = db.Accounts.Where(m => m.ID == ID).FirstOrDefault();
                    acc.FirstName = FirstName;
                    acc.LastName = LastName;
                    db.SaveChanges();
                    string name=acc.LastName + " "+ acc.FirstName; 
                    return Json(name, JsonRequestBehavior.AllowGet);
                }
            }
            catch
            {

            }
            return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult Email_Change(int ID, string Email)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var acc = db.Accounts.Where(m => m.ID == ID).FirstOrDefault();
                    acc.Email = Email;
                    db.SaveChanges();
                    return Json(acc.Email, JsonRequestBehavior.AllowGet);
                }
            }
            catch
            {

            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Check_Password(int ID, string password_old)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var acc = db.Accounts.Where(m => m.ID == ID).FirstOrDefault();
                    var mk_old = WebSecurity.Login(User.Identity.Name, password_old);
                    if (mk_old==true)
                    {
                        return Json("true", JsonRequestBehavior.AllowGet);
                    }
                    else
                        return Json("false", JsonRequestBehavior.AllowGet);
                }
            }
            catch
            {

            }
            return Json("false", JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Password_Change(int ID, string password_new)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var acc = db.Accounts.Where(m => m.ID == ID).FirstOrDefault();
                    var token = WebSecurity.GeneratePasswordResetToken(User.Identity.Name);
                    WebSecurity.ResetPassword(token, password_new);
                    db.SaveChanges();
                    return Json("true", JsonRequestBehavior.AllowGet);
                }
            }
            catch
            {

            }
            return Json("false", JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Birthday_Change(int ID, int Days, int Months, int Years)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var acc = db.Accounts.Where(m => m.ID == ID).FirstOrDefault();
                    acc.BirthDay = new DateTime(Years, Months, Days);
                    db.SaveChanges();
                    var birthday = acc.BirthDay.ToShortDateString();
                    return Json(birthday, JsonRequestBehavior.AllowGet);
                }
            }
            catch
            {

            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Gender_Change(int ID, bool Gender)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var acc = db.Accounts.Where(m => m.ID == ID).FirstOrDefault();
                    acc.Gender = Gender;
                    db.SaveChanges();
                    var gender = "";
                    if (acc.Gender == false)
                    {
                        gender = "Nam";
                    }
                    else
                    {
                        gender = "Nữ";
                    }
                    return Json(gender, JsonRequestBehavior.AllowGet);
                }
            }
            catch
            {

            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Phone_Change(int ID, string Phone)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var acc = db.Accounts.Where(m => m.ID == ID).FirstOrDefault();
                    acc.Phone = Phone;
                    db.SaveChanges();
                    return Json(acc.Phone, JsonRequestBehavior.AllowGet);
                }
            }
            catch
            {

            }
            return RedirectToAction("Index");
        }
    }
}
